﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using MallProject.Model;
using System.Configuration;
using System.Data;

namespace MallProject.Admin.Controller
{
    public class SettingController : IGeneral
    {
        private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
        private static SqlConnection _con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
        public void Insert(object obj)
        {
            throw new NotImplementedException();
        }

        public bool Update(object obj)
        {
            if (con.State == ConnectionState.Closed)
                con.Open();
            Settings set = (Settings)obj;
            SqlCommand cmd = new SqlCommand("UPDATE Settings SET name=@name,value=@value,[status]=@status WHERE id=@id", con);
            cmd.Parameters.AddWithValue("@name", set.Name);
            cmd.Parameters.AddWithValue("@value", set.Value);
            cmd.Parameters.AddWithValue("@status", set.Status);
            cmd.Parameters.AddWithValue("@id", set.Id);
            int i = cmd.ExecuteNonQuery();
            HttpContext.Current.Response.Write("i: " + set.Id);
            if (i > 0)
                return true;

            return false;
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        public void ListAll()
        {
            throw new NotImplementedException();
        }

        public void GetById(int id)
        {
            throw new NotImplementedException();
        }

        public void GetOption()
        {
            throw new NotImplementedException();
        }
        public static Settings getValue(string Name)
        {
            if (_con.State == ConnectionState.Closed)
            {
                _con.Open();
            }
            SqlCommand cmd = new SqlCommand("SELECT * FROM Settings WHERE name=@name", _con);
            cmd.Parameters.AddWithValue("@name", Name);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            Settings set = new Settings(dt.Rows[0]);
            return set;
        }
        public bool UpdateMulti(List<Settings> oList)
        {
            bool check = false;
            foreach (Settings item in oList)
            {
                check = Update(item);
            }
            return check;
        }
    }
}